package structure;

import java.util.Deque;
import java.util.LinkedList;

public class MyBinaryTree {
    private static class TreeNode {
        char val;
        TreeNode left;
        TreeNode right;

        TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        // 1.先创建结点，这与递归创建树逻辑顺序一致
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        // 2.将结点之间联系起来
        A.left = B;
        B.left = D;
        B.right = E;
        E.right = H;
        A.right = C;
        C.left = F;
        C.right = G;

        // 3.返回根结点
        return A;
    }

    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }

        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    public void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }

        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        while(!deque.isEmpty()) {
            TreeNode cur = deque.pop();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                deque.offer(cur.left);
            }
            if (cur.right != null) {
                deque.offer(cur.right);
            }
        }
    }

    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }

    public int getLeafNode(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNode(root.left) + getLeafNode(root.right);
    }

    public int getKLevelNode(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNode(root.left, k - 1) + getKLevelNode(root.right, k-1);

    }

    public int treeHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(treeHeight(root.left),treeHeight(root.right)) + 1;
    }

    public Boolean contains(char val, TreeNode root) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return true;
        }

        Boolean left = contains(val, root.left);
        if (left != null) {
            return left;
        }
        Boolean right = contains(val, root.right);
        if (right != null) {
            return right;
        }
        return null;
    }

    public static void main(String[] args) {
        MyBinaryTree tree = new MyBinaryTree();
        TreeNode root = tree.createTree();

        System.out.println(tree.size(root));
        System.out.println(tree.getLeafNode(root));
        System.out.println(tree.getKLevelNode(root,1));
        System.out.println(tree.treeHeight(root));
        System.out.println(tree.contains('H',root));
    }


    public static void main1(String[] args) {
        MyBinaryTree tree = new MyBinaryTree();
        TreeNode root = tree.createTree();
        tree.preOrder(root);
        System.out.println();
        tree.inOrder(root);
        System.out.println();
        tree.postOrder(root);
        System.out.println();
        tree.levelOrder(root);
        System.out.println();
    }


}
